package cn.dream.messengerserver;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.dream.ebag.MessengerLib.MGServer;


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    MGServer mgServer;
    List<String> clientList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mgServer = new MGServer(this) {
            @Override
            public void onNewClient(String clientPkg) {
                Log.d(TAG, "onNewClient: " + clientPkg);
                if (!clientList.contains(clientPkg)) {
                    clientList.add(clientPkg);
                }
            }

            @Override
            public void onUpdateClientList(List<String> list) {
                Log.d(TAG, "onUpdateClientList: " + Arrays.toString(list.toArray()));
                if (clientList == null) {
                    clientList = new ArrayList<>();
                }
                clientList.clear();
                clientList.addAll(list);
            }

            @Override
            public void onJsonMsg(String clientPkg, String message) {
                Log.d(TAG, "onJsonMsg: from " + clientPkg + ",msg=" + message);
            }

            @Override
            public void onErrorMsg(String clientPkg, String msg) {
                Log.d(TAG, "onJsonMsg: from " + clientPkg + ",msg=" + msg);
            }
        };
        mgServer.requestClientTagList();

        findViewById(R.id.send_json).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clientList != null && clientList.size() > 0) {
                    mgServer.sendJsonMsg2Client("this is a json message", clientList.get(0));
                }

            }
        });

        findViewById(R.id.send_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clientList != null && clientList.size() > 0) {
                    mgServer.sendErrorMsg2Client("this is a error message", clientList.get(0));
                }
            }
        });

        findViewById(R.id.close_connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clientList.size() > 0) {
                    mgServer.sendCloseClientMsg(clientList.get(0));
                }
            }
        });

    }


    /**
     * 判断连接的url地址返回的状态，必须在子线程运行否则报错null
     *
     * @param address
     * @return
     */
    public int getUrlConnectionState(String address) {
        int status = HttpURLConnection.HTTP_NOT_FOUND;
        try {
            URL urlObj = new URL(address);
            HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
            oc.setUseCaches(false);
            oc.setConnectTimeout(3000); // 设置超时时间
            oc.connect();
            status = oc.getResponseCode();// 请求状态
            if (HttpURLConnection.HTTP_OK == status) {
                return status;
            }
        } catch (Exception e) {
            Log.e(TAG, " Media getUrlConnectionState error=" + e.getMessage());
        }
        return status;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mgServer != null) {
            mgServer.closeConnection();
        }
    }
}
